﻿Namespace ViewModel

    Public Class SourceAddInViewModel
        Inherits AddInItemViewModelBase


        Private AddIn As Lazy(Of ISourceAddIn, IAddInMetaData)

        Public Sub New(ByVal addIn As Lazy(Of ISourceAddIn, IAddInMetaData))
            Me.AddIn = addIn
            Me.Metadata = addIn.Metadata
        End Sub

        Public Overrides Sub Activate()
            If Not AddIn.IsValueCreated Then
                AddIn.Value.Initialize(Me.Settings, Me.Helper)
            End If
            AddIn.Value.Activate()
            Me.IsEnabled = True
        End Sub

        Public Overrides Sub Deactivate()
            If Me.IsEnabled AndAlso AddIn.IsValueCreated Then
                AddIn.Value.Deactivate()
                Me.IsEnabled = False
            End If
        End Sub

        Public Overrides Function GetSettingControl() As Control
            If Me.HasSetting Then
                If Not AddIn.IsValueCreated Then
                    AddIn.Value.Initialize(Me.Settings, Me.Helper)
                End If
                Return AddIn.Value.GetSettingControl()
            Else
                Throw New NotSupportedException
            End If
        End Function

        Public Overrides Function GetContentControl(ByVal message As Message) As System.Windows.Controls.Control
            If Me.HasContent Then
                If Not AddIn.IsValueCreated Then
                    AddIn.Value.Initialize(Me.Settings, Me.Helper)
                End If
                Return AddIn.Value.GetContentControl(message)
            Else
                Throw New NotSupportedException
            End If
        End Function

        Public Overrides Function GetSidebarControl() As System.Windows.Controls.Control
            If Me.HasSidebar Then
                If Not AddIn.IsValueCreated Then
                    AddIn.Value.Initialize(Me.Settings, Me.Helper)
                End If
                Return AddIn.Value.GetSidebarControl
            Else
                Throw New NotSupportedException
            End If
        End Function

    End Class

End Namespace
